﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureUIEditModeUtil.h"
#include "UiEditModeUtils.h"
#include "GbmpControlIds.h"
#include "StructureCmdIds.h"
#include "IRibbonGroup.h"
#include "IGalleryDefinition.h"
#include "ICommandButtonDefinition.h"
#include "IRibbonContextualPage.h"
#include "GbmpUiPlatformCommandIds.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

#define DRAW_PROFILE_GROUP_ID                   L"DrawProfileDynamicGroup"
#define DRAW_PROFILE_TYPE_GALLERY_ID            L"DrawProfileGallery"

namespace
{
    // 基于新UI框架实现的Gallery
    OwnerPtr<IGalleryDefinition> CreateDrawGalleryDefinition(const std::vector<std::wstring>& cmdStrings,
        const std::vector<std::wstring>& names, const std::vector<std::wstring>& icons)
    {
        OwnerPtr<IGalleryDefinition> opGalleryDef = IGalleryDefinition::Create(DRAW_PROFILE_TYPE_GALLERY_ID, GBMP_TR(L"绘制方式"));
        for (size_t i = 0; i < cmdStrings.size(); ++i)
        {
            opGalleryDef->AddItem(cmdStrings[i], names[i], names[i], icons[i], cmdStrings[i]);
        }

        if (cmdStrings.size() > 0)
            opGalleryDef->SetCurrentGalleryItem(cmdStrings[0]);

        return opGalleryDef;
    }
}

void StructureUIEditModeUtil::SetupDrawProfileContext(gcmp::IRibbonContextualPage *pContextualPage, const std::wstring& contextualPageCaption)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pContextualPage, L"pContextualPage为空",L"GDMPLab",L"2024-03-30");

    pContextualPage->DeleteAllGroups();

    std::vector<std::wstring> cmdStrings;
    std::vector<std::wstring> names;
    std::vector<std::wstring> icons;
    cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_STRAIGHT_PROFILE); names.push_back(GBMP_TR(L"直线")); icons.push_back(L":/images/sketchline/直线16px.png");
    cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_RECT_PROFILE); names.push_back(GBMP_TR(L"矩形")); icons.push_back(L":/images/sketchline/矩形16px.png");
    cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_ARC_PROFILE); names.push_back(GBMP_TR(L"圆弧")); icons.push_back(L":/images/sketchline/三点圆弧16px.png");
    cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_PROFILE_BY_PICK); names.push_back(GBMP_TR(L"拾取线")); icons.push_back(L":/images/sketchline/选择线16px.png");

    OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(DRAW_PROFILE_GROUP_ID, GBMP_TR(L"绘制轮廓"));
    {
        OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(cmdStrings, names, icons);
        opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
    }

    pContextualPage->AddGroup(ID_PAGE_GBMP_MODIFY, TransferOwnership(opDrawGroup), contextualPageCaption);
}

void StructureUIEditModeUtil::SetupDrawSlopeProfileContext(gcmp::IRibbonContextualPage *pContextualPage, gcmp::IUiEditModeBehavior *pUIEditMode,
    const std::wstring& contextualPageCaption, bool isDrawingSlopeLine)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pContextualPage, L"pContextualPage为空",L"GDMPLab",L"2024-03-30");

    pContextualPage->DeleteAllGroups();

    OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(DRAW_PROFILE_GROUP_ID, GBMP_TR(L"绘制轮廓"));
    {
        OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_STRUCTURE_SHOW_FLOOR_PROFILE_SLOPE_RIBBON_CONTEXT, GBMP_TR(L"楼板轮廓"), GBMP_TR(L"楼板轮廓"),
            ID_CMD_STRUCTURE_SHOW_FLOOR_PROFILE_SLOPE_RIBBON_CONTEXT, L":/images/structure/楼板轮廓.png", ButtonStyle::TextUnderIcon);
        opDrawGroup->AddButton(TransferOwnership(opBtnDef));
    }
    {
        OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_STRUCTURE_DRAW_SLOPE, GBMP_TR(L"坡度线"), GBMP_TR(L"坡度线"),
            ID_CMD_STRUCTURE_DRAW_SLOPE, L":/images/structure/坡度线.png", ButtonStyle::TextUnderIcon);
        opDrawGroup->AddButton(TransferOwnership(opBtnDef));
    }

    if (!isDrawingSlopeLine)
    {
        std::vector<std::wstring> cmdStrings;
        std::vector<std::wstring> names;
        std::vector<std::wstring> icons;
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_STRAIGHT_PROFILE); names.push_back(GBMP_TR(L"直线")); icons.push_back(L":/images/sketchline/直线16px.png");
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_RECT_PROFILE); names.push_back(GBMP_TR(L"矩形")); icons.push_back(L":/images/sketchline/矩形16px.png");
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_ARC_PROFILE); names.push_back(GBMP_TR(L"圆弧")); icons.push_back(L":/images/sketchline/三点圆弧16px.png");
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_PROFILE_BY_PICK); names.push_back(GBMP_TR(L"拾取线")); icons.push_back(L":/images/sketchline/选择线16px.png");

        OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(cmdStrings, names, icons);
        opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
    }
    else
    {
        std::vector<std::wstring> cmdStrings;
        std::vector<std::wstring> names;
        std::vector<std::wstring> icons;
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_SLOPE); names.push_back(GBMP_TR(L"坡度线")); icons.push_back(L":/images/sketchline/直线16px.png");

        OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(cmdStrings, names, icons);
        opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
    }

    pContextualPage->AddGroup(ID_PAGE_GBMP_MODIFY, TransferOwnership(opDrawGroup), contextualPageCaption);
}

void StructureUIEditModeUtil::SetupDrawRoofProfileContext(gcmp::IRibbonContextualPage *pContextualPage, gcmp::IUiEditModeBehavior *pUIEditMode,
    const std::wstring& contextualPageCaption, bool isDrawingSlopeLine)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pContextualPage, L"pContextualPage为空",L"GDMPLab",L"2024-03-30");

    pContextualPage->DeleteAllGroups();

    OwnerPtr<IRibbonGroup> opDrawGroup = IRibbonGroup::Create(DRAW_PROFILE_GROUP_ID, GBMP_TR(L"绘制轮廓"));
    {
        OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_STRUCTURE_SHOW_ROOF_PROFILE_SLOPE_RIBBON_CONTEXT, GBMP_TR(L"屋顶轮廓"), GBMP_TR(L"屋顶轮廓"),
            ID_CMD_STRUCTURE_SHOW_ROOF_PROFILE_SLOPE_RIBBON_CONTEXT, L":/images/structure/楼板轮廓.png", ButtonStyle::TextUnderIcon);
        opDrawGroup->AddButton(TransferOwnership(opBtnDef));
    }
    {
        OwnerPtr<ICommandButtonDefinition> opBtnDef = ICommandButtonDefinition::Create(ID_CMD_STRUCTURE_DRAW_SLOPE, GBMP_TR(L"坡度线"), GBMP_TR(L"坡度线"),
            ID_CMD_STRUCTURE_DRAW_SLOPE, L":/images/structure/坡度线.png", ButtonStyle::TextUnderIcon);
        opDrawGroup->AddButton(TransferOwnership(opBtnDef));
    }

    if (!isDrawingSlopeLine)
    {
        std::vector<std::wstring> cmdStrings;
        std::vector<std::wstring> names;
        std::vector<std::wstring> icons;
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_ROOF_STRAIGHT_PROFILE); names.push_back(GBMP_TR(L"直线")); icons.push_back(L":/images/sketchline/直线16px.png");

        OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(cmdStrings, names, icons);
        opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
    }
    else
    {
        std::vector<std::wstring> cmdStrings;
        std::vector<std::wstring> names;
        std::vector<std::wstring> icons;
        cmdStrings.push_back(ID_CMD_STRUCTURE_DRAW_SLOPE); names.push_back(GBMP_TR(L"直线")); icons.push_back(L":/images/sketchline/直线16px.png");

        OwnerPtr<IGalleryDefinition> opGalleryDef = CreateDrawGalleryDefinition(cmdStrings, names, icons);
        opDrawGroup->AddGallery(TransferOwnership(opGalleryDef));
    }

    pContextualPage->AddGroup(ID_PAGE_GBMP_MODIFY, TransferOwnership(opDrawGroup), contextualPageCaption);
}

bool StructureUIEditModeUtil::GetCommandAvailability(const std::wstring& commandId)
{
    if (commandId == ID_CMD_GBMP_CUT_ELEMENT)
    {
        return false;
    }

    if (commandId == ID_CMD_DRAW_STRUCTURE_STRAIGHT_WALL
        || commandId == ID_CMD_DRAW_STRUCTURE_ARC_WALL
        || commandId == ID_CMD_DRAW_VARIABLE_SECTION_STRUCTURE_STRAIGHT_WALL
        || commandId == ID_CMD_DRAW_STRUCTURE_VERTICAL_COLUMN
        || commandId == ID_CMD_DRAW_STRUCTURE_SLANTED_COLUMN
        || commandId == ID_CMD_CREATE_STRAIGHT_STRUCTURE_BEAM
        || commandId == ID_CMD_CREATE_STRAIGHT_STRUCTURE_VARIABLE_SECTION_BEAM
        || commandId == ID_CMD_CREATE_ARC_3PTS_STRUCTURE_BEAM
        || commandId == ID_CMD_CREATE_ARC_3PTS_STRUCTURE_VARIABLE_SECTION_BEAM
        || commandId == ID_CMD_CREATE_STRUCTURE_FLOOR
        || commandId == ID_CMD_CREATE_RAFT_FOUNDATION
        || commandId == ID_CMD_CREATE_STRUCTURE_PLATE_HOLE
        || commandId == ID_CMD_DRAW_RECT_STRUCTURE_WALL_HOLE
        || commandId == ID_CMD_CREATE_STRAIGHT_STRUCTURE_STRIP_FOUNDATION)
    {
        return false;
    }

    return true;
}
